package com.xhwl.logistics.strategy.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.utils.ChkUtil;
import com.xhwl.logistics.bo.VehicleParkingLotSecondBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.manipulate.CreateVehicleCarBO;
import com.xhwl.logistics.bo.manipulate.DeleteVehicleCarBO;
import com.xhwl.logistics.bo.manipulate.UpdateVehicleCarBO;
import com.xhwl.logistics.dto.CarDTO;
import com.xhwl.logistics.dto.CarLotListDTO;
import com.xhwl.logistics.dto.CardInfoDTO;
import com.xhwl.logistics.dto.PlateNoInfoDTO;
import com.xhwl.logistics.dto.http.CarAddKeTopDTO;
import com.xhwl.logistics.dto.http.CarAuditKeTopDTO;
import com.xhwl.logistics.dto.http.query.QueryCarCardInfoDTO;
import com.xhwl.logistics.entity.VehicleCar;
import com.xhwl.logistics.entity.VehicleRelationCarToCard;
import com.xhwl.logistics.service.impl.VehicleCarServiceImpl;
import com.xhwl.logistics.service.impl.VehicleParkingLotServiceImpl;
import com.xhwl.logistics.service.impl.VehicleRelationCarToCardServiceImpl;
import com.xhwl.logistics.service.impl.http.KeTopHttpServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotCarAuthHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author msy
 */
//@Service
@Slf4j
public class VehicleParkingLotKeTopCarAuthHandlerImpl implements IVehicleParkingLotCarAuthHandler  {

    @Autowired
    private KeTopHttpServiceImpl keTopHttpService;

    @Autowired
    private VehicleParkingLotServiceImpl vehicleParkingLotService;

    @Autowired
    private VehicleCarServiceImpl vehicleCarService;

    @Autowired
    private VehicleRelationCarToCardServiceImpl vehicleRelationCarToCardService;

    @Override
    public void addCar(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, CreateVehicleCarBO createVehicleCarBO) {
        Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();
        VehicleParkingLotSecondBO vehicleParkingLotSecondBO = vehicleParkingLotService.queryParkingLot(projectId, createVehicleCarBO.getParkingLotName());
        List<VehicleCar> vehicleCarList = vehicleCarService.list(new LambdaQueryWrapper<VehicleCar>()
                .eq(VehicleCar::getProjectId, projectId)
                .eq(VehicleCar::getParkingLotId, vehicleParkingLotSecondBO.getParkId())
                .eq(VehicleCar::getPlateNumber, createVehicleCarBO.getPlateNumber()));

        if(ChkUtil.isNotNull(vehicleCarList)) {
            throw new BusinessException("该车场下已存在该车辆");
        }

        CarAddKeTopDTO carAddKeTopDTO = toCarAddKeTopDTO(createVehicleCarBO);
        String resultJson = keTopHttpService.addCarCardNo(vehicleIntegratedConfigBO, carAddKeTopDTO);
        JSONObject data = new JSONObject(resultJson);
        Integer cardId = data.getInt("cardId");
        Integer isCheck = data.getInt("ischeck");
        Integer passCardId = cardId;

        // isCheck = 0代表未审核，需要调用固定车审批接口
        if(Integer.valueOf(0).equals(isCheck)) {
            CarAuditKeTopDTO carAuditKeTopDTO = new CarAuditKeTopDTO();
            carAuditKeTopDTO.setUserId(createVehicleCarBO.getCreatorAccountId());
            carAuditKeTopDTO.setUserName(createVehicleCarBO.getCreatorAccountName());
            carAuditKeTopDTO.setAuditType(1);
            carAuditKeTopDTO.setCardId(cardId);
            carAuditKeTopDTO.setAuditStatus(1);
            carAuditKeTopDTO.setAuditRemark("审批通过");
            resultJson = keTopHttpService.auditCardRecharge(vehicleIntegratedConfigBO, carAuditKeTopDTO);
            data = new JSONObject(resultJson);
            passCardId = data.getInt("passCardId");
        }
        createVehicleCarBO.setCardId(passCardId);
    }


    /**
     * 构建新增固定车辆参数
     * @param createVehicleCarBO
     * @return
     */
    public CarAddKeTopDTO toCarAddKeTopDTO(CreateVehicleCarBO createVehicleCarBO) {
        CardInfoDTO cardInfoDTO = new CardInfoDTO();
        cardInfoDTO.setCardName(createVehicleCarBO.getOwnerName());
        cardInfoDTO.setUseName(createVehicleCarBO.getOwnerName());
        cardInfoDTO.setTel(createVehicleCarBO.getOwnerPhone());

        List<CarLotListDTO> carLotListDTOList = new ArrayList<>();
        CarLotListDTO carLotListDTO = new CarLotListDTO();
        carLotListDTO.setLotName(createVehicleCarBO.getOwnerName() + "车位");
        carLotListDTO.setCarType(createVehicleCarBO.getCardType());
        carLotListDTO.setSequence(1);
        carLotListDTO.setAreaName("所有区域");
        carLotListDTO.setAreaId(Arrays.asList("-1"));
        carLotListDTO.setLotCount(1);
        carLotListDTOList.add(carLotListDTO);

        List<PlateNoInfoDTO> plateNoInfoDTOList = new ArrayList<>();
        PlateNoInfoDTO plateNoInfoDTO = new PlateNoInfoDTO();
        plateNoInfoDTO.setPlateNo(createVehicleCarBO.getPlateNumber());
        plateNoInfoDTOList.add(plateNoInfoDTO);

        CarAddKeTopDTO carAddKeTopDTO = new CarAddKeTopDTO();
        carAddKeTopDTO.setUserId(createVehicleCarBO.getCreatorAccountId());
        carAddKeTopDTO.setUserName(createVehicleCarBO.getCreatorAccountName());
        carAddKeTopDTO.setCardInfo(JSON.toJSONString(cardInfoDTO));
        carAddKeTopDTO.setCarLotList(JSON.toJSONString(carLotListDTOList));
        carAddKeTopDTO.setPlateNoInfo(JSON.toJSONString(plateNoInfoDTOList));
        return carAddKeTopDTO;
    }



    @Override
    public void deleteCar(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, String carDataStr) {
        Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();
        DeleteVehicleCarBO deleteVehicleCarBO = JSONUtil.toBean(carDataStr, DeleteVehicleCarBO.class);

        LambdaQueryWrapper<VehicleRelationCarToCard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(VehicleRelationCarToCard::getProjectId, projectId)
                .eq(VehicleRelationCarToCard::getCarId, deleteVehicleCarBO.getId());
        VehicleRelationCarToCard vehicleRelationCarToCard = vehicleRelationCarToCardService.getOne(lambdaQueryWrapper);
        if(vehicleRelationCarToCard != null) {
            String cardId = String.valueOf(vehicleRelationCarToCard.getCardId());
            keTopHttpService.delCarCardInfo(vehicleIntegratedConfigBO, cardId);
        }
    }

    @Override
    public void updateCar(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, String carDataStr) {
        UpdateVehicleCarBO updateVehicleCarBO = JSONUtil.toBean(carDataStr, UpdateVehicleCarBO.class);

        Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();
        LambdaQueryWrapper<VehicleRelationCarToCard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(VehicleRelationCarToCard::getProjectId, projectId)
                .eq(VehicleRelationCarToCard::getCarId, updateVehicleCarBO.getId());
        VehicleRelationCarToCard vehicleRelationCarToCard = vehicleRelationCarToCardService.getOne(lambdaQueryWrapper);

        if(vehicleRelationCarToCard != null) {
            Integer cardId = vehicleRelationCarToCard.getCardId();
            QueryCarCardInfoDTO queryCarCardInfoDTO = new QueryCarCardInfoDTO();
            queryCarCardInfoDTO.setCardId(cardId);
            CarDTO carDTO = keTopHttpService.getCarCardInfo(vehicleIntegratedConfigBO, queryCarCardInfoDTO);

            CardInfoDTO cardInfoDTO = new CardInfoDTO();
            cardInfoDTO.setCardId(cardId);
            cardInfoDTO.setUseName(updateVehicleCarBO.getOwnerName());
            cardInfoDTO.setTel(updateVehicleCarBO.getOwnerPhone());

            List<CarLotListDTO> carLotListDTO = new ArrayList<>();
            BeanUtils.copyProperties(carDTO.getCarLotList(), carLotListDTO);
            carLotListDTO.get(0).setCarType(updateVehicleCarBO.getCarType());
            carLotListDTO.get(0).setLotName(updateVehicleCarBO.getOwnerName() + "车位");
            carLotListDTO.get(0).setUpdateTime(DateUtil.now());

            List<PlateNoInfoDTO> plateNoInfoDTO = new ArrayList<>();
            BeanUtils.copyProperties(carDTO.getPlateNoInfo(), plateNoInfoDTO);
            plateNoInfoDTO.get(0).setPlateNo(updateVehicleCarBO.getPlateNumber());

            CarAddKeTopDTO carAddKeTopDTO = new CarAddKeTopDTO();
            carAddKeTopDTO.setUserId(updateVehicleCarBO.getOwnerAccountId());
            carAddKeTopDTO.setUserName(updateVehicleCarBO.getOwnerName());
            carAddKeTopDTO.setCardInfo(JSON.toJSONString(cardInfoDTO));
            carAddKeTopDTO.setCarLotList(JSON.toJSONString(carLotListDTO));
            carAddKeTopDTO.setPlateNoInfo(JSON.toJSONString(plateNoInfoDTO));

            keTopHttpService.modifyCarCardNo(vehicleIntegratedConfigBO, carAddKeTopDTO);
        }
    }
}
